package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Product;
import com.sjlh.centurylianhua.entity.ProductType;
import com.sjlh.centurylianhua.entity.Supplier;
import com.sjlh.centurylianhua.mapper.SupplierMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierMapper supplierMapper;

    /**
     * 获取所有供应商
     * @return
     */
    /*@RequestMapping("/showAll")
    public Object show(){
        return supplierMapper.selectList(null);
    }*/

    /**
     * 多条件分页查询
     * @param sc
     * @return
     */
    @RequestMapping("/show")
    public Object showAllByCon(@RequestBody SearchCon sc) {
        // 封装条件查询构造器
        QueryWrapper<Supplier> wrapper = new QueryWrapper<Supplier>();
        wrapper.like(StringUtils.isnotNull(sc.getName()), "supplier.name", sc.getName());
        wrapper.like(StringUtils.isnotNull(sc.getAddress()), "supplier.address", sc.getAddress());
        wrapper.eq(StringUtils.isnotNull(sc.getState()), "supplier.state", sc.getState());
        //追加多表连接的sql语句
        wrapper.apply("supplier.typeId = producttype.id");
        if (sc.getPage() != null) {
            //代表分页
            Page<Supplier>supplierPage = new Page<Supplier>(sc.getPage(), sc.getLimit());
            //查询//调用mapper添加的多表连接方法查询进货日志详情
            List<Supplier> list = supplierMapper.getInfoPage(supplierPage, wrapper);
            return new ResultData(true, 20000, "成功", list, supplierPage.getTotal());
        } else {
            // 数据为空时返回的所有数据
            List<Supplier> list=supplierMapper.getInfo(wrapper);
            long count = supplierMapper.getInfo(wrapper).size();
            return new ResultData(true, 20000, "成功", list, count);
        }
    }

    /**
     *根据供应商名字精确查询
     * */
    public Supplier getOne(@RequestBody Supplier supplier){
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        wrapper.eq( "name", supplier.getName());
        wrapper.eq( "state", supplier.getState());
        return supplierMapper.selectOne(wrapper);
    }

    /**
     * 添加供应商
     * @param supplier
     * @return
     */
    @RequestMapping("/add")
    public Object addSupplier(@RequestBody Supplier supplier) {
        Supplier s = this.getOne(supplier);
        if (s == null) {
            int r = supplierMapper.insert(supplier);
            if (r == 1)
                return new ResultData(true, 20000, "添加成功", true, null);
            else
                return new ResultData(false, 20000, "添加失败", false, null);
        }else {
                return new ResultData(false,20001,"该供应商已存在",false,null);
            }
    }

    /**
     * 更新供应商
     * @param supplier
     * @return
     */
    @RequestMapping("/update")
    public Object updateSupplier(@RequestBody Supplier supplier){
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        wrapper.eq( "name", supplier.getName());
        wrapper.ne("id",supplier.getId());
        Supplier sup=supplierMapper.selectOne(wrapper);
        if (sup== null) {
            int r = supplierMapper.updateById(supplier);
            if (r == 1)
                return new ResultData(true, 20000, "修改成功", true, null);
            else
                return new ResultData(false, 20000, "修改失败", false, null);
        }else {
            return new ResultData(false,20001,"该供应商已存在",false,null);
        }
    }

    /**
     * 删除供应商
     * @param supplier
     * @return
     */
    @RequestMapping("/del")
    public Object delSupplier(@RequestBody Supplier supplier){
        int r= supplierMapper.deleteById(supplier.getId());
        if(r==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);

    }

    /**
     * 批量删除供应商
     * @param ids
     * @return
     */
    @RequestMapping("/batchDel")
    public Object batchDelSupplier(List<Integer> ids){
        int r= supplierMapper.deleteBatchIds(ids);
        return r==1?true:false;
    }

}
